package com.kongmou.springReview.advanced.springAdware;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

@Service
public class AdwareService implements BeanNameAware, ResourceLoaderAware {

	private String beanName;

	private ResourceLoader resourceLoader;

	@Override
	public void setResourceLoader(ResourceLoader resourceLoader) {

		this.resourceLoader = resourceLoader;
	}

	@Override
	public void setBeanName(String name) {
		this.beanName = name;
	}

	public void outputResult() {

		System.out.println("Bean的名称为：" + beanName);

		Resource resource = resourceLoader.getResource("classpath:test.txt");

		try {
			System.out.println("Resouceloader加载的内容为：" + IOUtils.toString(resource.getInputStream(), "utf-8"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
